<?php
namespace Dynamic\Controller;
use Common\Controller\CommonController;

/**
 * 动态模块
 */
class DynamicController extends CommonController {
	protected $_dynamic;
	protected $_dynamic_reply;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 105;
		$this->_dynamic = new \Dynamic\Model\DynamicModel();
		$this->_dynamic_reply = new \Dynamic\Model\DynamicReplyModel();
	}
	
	private function _getTargetPushCusts(){
		$targetCusts = array();
		$friendList = $this->_getFriendList($this->_custInfo['custid']);
		if(!empty($friendList)){//对好友进行提示
			foreach($friendList as &$friend){
				$targetCusts[] = $friend['custid'];
			}
		}
			
		//TODO改为redis集合缓存取同组织成员 
		if(empty($this->_ent)) $this->_ent = new \Common\Model\EntModel();
		$entList = $this->_ent->getEntByCust($this->_custInfo['custid']);
		if(!empty($entList)){
			if(empty($this->_ent_cust)) $this->_ent_cust = new \Common\Model\EntCustModel();
			foreach($entList as $ent){
				$entid = $ent['entid'];
				$custList = $this->_ent_cust->getCustByEnt($entid);
				if(!empty($custList)){
					foreach($custList as $cust){
						$targetCusts[] = $cust['custid'];
					}
				}
			}
		}
		
		if(!empty($targetCusts)){
			$targetCusts = array_unique($targetCusts);
		}
		
		return $targetCusts;
	}
	
    
    /**
     * 发布动态
     */
	public function addDynamicAction(){
		$content = I('request.content');
		$files = I('request.files');
        $video = I('request.video');
        $video_cover = I('request.video_cover');
    	$this->_addDynamic($content ,$video,$video_cover, $files);
	}
	
	private function _addDynamic($content ,$video,$video_cover, $files="" , $visible=1){
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['content'] = $content;
		$data['files'] = $files;
		$data['visible'] = $visible;
        $data['video'] = $video;
        $data['video_cover'] = $video_cover;
		$dynamicid = $this->_dynamic->addDynamic($data);
		if($dynamicid){
			$friendCust = $this->_getTargetPushCusts();
			if(!empty($friendCust)){
				$friendCust = array_unique($friendCust);
			
				$start = 0;
				$step = 600;
				$friendsTmp = array_slice($friendCust, $start, $step);
				while(!empty($friendsTmp)){
					//小红点提示好友有新的动态发布
					$msg['from_cust_name'] = $this->_custInfo['nickname'];
					$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
					$msg['type'] = 40;
					$msg['content'] = "";
					\im::sendMsg(3, $friendsTmp, array(), $msg , 1);
					
					$start = $start + $step;
					$friendsTmp = array_slice($friendCust, $start, $step);
				}
			}
			
			$this->outPut();
		}else{
			$this->status = 1601;
			$this->msg = L('STATUS_1601');
			$this->outPut();
		}
	}
	
	
	
	private function _getReplyByDynamicId($dynamicid , $friendcusts = array()){
		$dynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['DYNAMIC_REPLY'].$dynamicid;
		$replyInfo = $this->_mycache->get($dynamicReplyKey);
		
		if(empty($replyInfo)){
			$replyInfo = $this->_dynamic_reply->getReplyByDynamicId($dynamicid);
			if(!empty($replyInfo)){
				foreach($replyInfo as &$reply){
					$custInfo = $this->_getCustInfo($reply['custid']);
					$reply['nickname'] = $custInfo['nickname'];
						
					if(!empty($reply['pcustid'])){
						$custInfo = $this->_getCustInfo($reply['pcustid']);
						$reply['pnickname'] = $custInfo['nickname'];
					}else{
						$reply['pnickname'] = "";
					}
				}
				
				$this->_mycache->set($dynamicReplyKey , $replyInfo);
		    	$this->_mycache->expire($dynamicReplyKey , C('DYNAMIC_REPLY_EXPIRE'));
			}else{
				$replyInfo = array();
			}
		}
		
		$returnData = array('replylist'=>array() , 'praiselist'=>array());
		if(!empty($replyInfo)){
			foreach($replyInfo as &$reply){
				if($reply['type'] == "1"){
					if(in_array($reply['custid'] , $friendcusts) && (in_array($reply['pcustid'] , $friendcusts) || $reply['pcustid'] == "0"))//滤掉非好友的回复
						$returnData['replylist'][] = array('replyid'=>$reply['replyid'],'custid'=>$reply['custid'],'nickname'=>$reply['nickname'],'pcustid'=>$reply['pcustid'],'pnickname'=>$reply['pnickname'],'content'=>$reply['content'],'createtime'=>$reply['createtime']);
				}elseif($reply['type'] == "2"){
					if(in_array($reply['custid'] , $friendcusts))//滤掉非好友的赞
						$returnData['praiselist'][] = array('custid'=>$reply['custid'],'nickname'=>$reply['nickname']);
				}
			}
		}
		
		return $returnData;
	}
	
	
	/**
	 * 获取好友动态列表
	 */
	public function getDynamicListAction(){
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_getDynamicList($pageIndex , $pageSize);
	}
	
	private function _getDynamicList($pageIndex = 0 , $pageSize = 0){
		$custids = array();
		//##################################################
		//仅仅看好友或同事或自己的动态，如不想限制，请把井号之间的代码注释
		$custids = $this->_getTargetPushCusts();
		$custids[] = $this->_custInfo['custid'];
		//##################################################
		
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custids'] = $custids;
		$result = $this->_dynamic->getDynamicList($data);
		if(!empty($result['data'])){
			foreach($result['data'] as &$dynamic){
				$custInfo = $this->_getCustInfo($dynamic['custid']);
				$dynamic['nickname'] = $custInfo['nickname'];
				
				if(!empty($dynamic['files'])) $dynamic['files'] = json_decode($dynamic['files'] , true);
                if(!empty($dynamic['video'])) $dynamic['video'] = json_decode($dynamic['video'] , true);
                if(!empty($dynamic['video_cover'])) $dynamic['video_cover'] = json_decode($dynamic['video_cover'] , true);
				
				$replyData = $this->_getReplyByDynamicId($dynamic['dynamicid'] , $custids);
				
				//回复列表
				$dynamic['replylist'] = $replyData['replylist'];
				
				//赞的列表
				$dynamic['praiselist'] = $replyData['praiselist'];
				$dynamic['_praise'] = "0";
				if(!empty($dynamic['praiselist'])){
					foreach($dynamic['praiselist'] as &$praise){
						if($praise['custid'] == $this->_custInfo['custid'])$dynamic['_praise'] = "1";
					}
				}
			}
		}else{
			$result['data'] = array();
		}
		
		$this->data = $result['data'];
		$this->outPut();
	}
	
	
	/**
	 * 获取单个好友动态
	 */
	public function getDynamicByCustAction(){
		$custid = (int)I('request.custid');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_getDynamicByCust($custid , $pageIndex , $pageSize);
	}
	
	private function _getDynamicByCust($custid , $pageIndex = 0 , $pageSize = 0){
		if(empty($custid)){
			$custid = $this->_custInfo['custid'];
		}
		
		$custids = array();
		//##################################################
		//仅仅看好友或同事或自己的动态，如不想限制，请把井号之间的代码注释
		$custids = $this->_getTargetPushCusts();
		$custids[] = $this->_custInfo['custid'];
		
		if(!in_array($custid , $custids)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		//##################################################
		
		$custInfo = $this->_getCustInfo($custid);
		$returnData['dynamicsetting'] = $custInfo['dynamicsetting'];
		$nickname = $custInfo['nickname'];
		
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custid'] = $custid;
		$result = $this->_dynamic->getDynamicList($data);
		if(!empty($result['data'])){
			foreach($result['data'] as &$dynamic){
				$dynamic['nickname'] = $nickname;
				
				if(!empty($dynamic['files'])) $dynamic['files'] = json_decode($dynamic['files'] , true);
                if(!empty($dynamic['video'])) $dynamic['video'] = json_decode($dynamic['video'] , true);
                if(!empty($dynamic['video_cover'])) $dynamic['video_cover'] = json_decode($dynamic['video_cover'] , true);
                $replyData = $this->_getReplyByDynamicId($dynamic['dynamicid'] , $custids);
				
				//回复列表
				$dynamic['replylist'] = $replyData['replylist'];
				
				//赞的列表
				$dynamic['praiselist'] = $replyData['praiselist'];
				$dynamic['_praise'] = "0";
				if(!empty($dynamic['praiselist'])){
					foreach($dynamic['praiselist'] as $praise){
						if($praise['custid'] == $this->_custInfo['custid'])$dynamic['_praise'] = "1";
					}
				}
			}
			
			$returnData['list'] = $result['data'];
		}else{
			$returnData = array();
		}
		
		$this->data = $returnData;
		$this->outPut();
	}
	
	
	/**
	 * 获取单个动态
	 */
	public function getDynamicDetailAction(){
		$dynamicid = (int)I('request.dynamicid');
		$this->_getDynamicDetail($dynamicid);
	}
	
	private function _getDynamicDetail($dynamicid){
		if(empty($dynamicid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
	
		$dynamic = $this->_dynamic->getDynamicById($dynamicid);
		if(empty($dynamic)){
			$this->status = 1607;
			$this->msg = L('STATUS_1607');
			$this->outPut();
		}
		
		$custids = array();
		//##################################################
		//仅仅看好友或同事或自己的动态，如不想限制，请把井号之间的代码注释
		$custids = $this->_getTargetPushCusts();
		$custids[] = $this->_custInfo['custid'];
		
		$custid = $dynamic['custid'];
		if(!in_array($custid , $custids)){
			$this->status = 1607;
			$this->msg = L('STATUS_1607');
			$this->outPut();
		}
		//##################################################
		

		$custInfo = $this->_getCustInfo($custid);
		$dynamic['nickname'] = $custInfo['nickname'];

		if(!empty($dynamic['files'])) $dynamic['files'] = json_decode($dynamic['files'] , true);
        if(!empty($dynamic['video'])) $dynamic['video'] = json_decode($dynamic['video'] , true);
        if(!empty($dynamic['video_cover'])) $dynamic['video_cover'] = json_decode($dynamic['video_cover'] , true);
        $replyData = $this->_getReplyByDynamicId($dynamic['dynamicid'] , $custids);
		
		//回复列表
		$dynamic['replylist'] = $replyData['replylist'];
		
		//赞的列表
		$dynamic['praiselist'] = $replyData['praiselist'];
		$dynamic['_praise'] = "0";
		if(!empty($dynamic['praiselist'])){
			foreach($dynamic['praiselist'] as &$praise){
				if($praise['custid'] == $this->_custInfo['custid'])$dynamic['_praise'] = "1";
			}
		}
		
		$this->data = $dynamic;
		$this->outPut();
	}
	
	
	/**
	 * 删除动态
	 */
	public function deleteDynamicAction(){
		$dynamicid = (int)I('request.dynamicid');
		$this->_deleteDynamic($dynamicid);
	}
	
	
	private function _deleteDynamic($dynamicid){
		if(empty($dynamicid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data = array();
		$data['dynamicid'] = $dynamicid;
		$data['custid'] = $this->_custInfo['custid'];
		$ret = $this->_dynamic->deleteDynamic($data);
		if($ret){
			$dynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['DYNAMIC_REPLY'].$dynamicid;
			$this->_mycache->rm($dynamicReplyKey);
			
			$this->outPut();
		}else{
			$this->status = 1615;
			$this->msg = L('STATUS_1615');
			$this->outPut();
		}
	}
	
	
	/**
	 * 赞或取消
	 */
	public function praiseAction(){
		$dynamicid = (int)I('request.dynamicid');
		$op = (int)I('request.op');
		$this->_praise($dynamicid , $op);
	}
	
	private function _praise($dynamicid , $op = 1){
		if(empty($dynamicid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$dynamicInfo = $this->_dynamic->getDynamicById($dynamicid);
		
		$data = array();
		$data['dynamicid'] = $dynamicid;
		$data['custid'] = $this->_custInfo['custid'];
		$data['type'] = 2;
		$data['status'] = $op == 1 ? $op : 0;
		if($op){
			$ret = $this->_dynamic_reply->addPraise($data);
			if($ret && $dynamicInfo['custid']!=$this->_custInfo['custid']){//对好友进行提示
				//与custid相关的赞
				$relationDynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_DYNAMIC_REPLY'].$dynamicInfo['custid'];
				$this->_mycache->sadd($relationDynamicReplyKey , $ret);
				
				$relationDynamicPraiseKey = \cachekey::$key['BASE_KEY_CONF']['CUST_DYNAMIC_PRAISE'].$this->_custInfo['custid'].":".$dynamicInfo['custid'];
				$praised = $this->_mycache->get($relationDynamicPraiseKey);
				if($praised != $this->_custInfo['custid'].":".$dynamicInfo['custid']){//从未赞过就发系统消息，曾经赞过就不发系统消息
					$this->_mycache->set($relationDynamicPraiseKey , $this->_custInfo['custid'].":".$dynamicInfo['custid']);
					//小红点提示好友有新的相关动态回复或赞
					$msg['from_cust_name'] = $this->_custInfo['nickname'];
					$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
					$msg['type'] = 41;
					$msg['content'] = "";
					\im::sendMsg(3, array($dynamicInfo['custid']), array(), $msg , 1);
				}
			}
		}else{
			$ret = $this->_dynamic_reply->deletePraise($data);
		}
		
		
		if($ret){
			//删除动态赞列表缓存
			$dynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['DYNAMIC_REPLY'].$dynamicid;
			$this->_mycache->rm($dynamicReplyKey);
			
			$this->outPut();
		}else{
			$this->status = 1609;
			$this->msg = L('STATUS_1609');
			$this->outPut();
		}
		
		
	}
	
	
	
	/**
	 * 发表回复
	 */
	public function addReplyAction(){
		$dynamicid = (int)I('request.dynamicid');
		$content = I('request.content');
		$pcustid = (int)I('request.pcustid');
		$this->_addReply($dynamicid , $content , $pcustid);
	}
	
	private function _addReply($dynamicid , $content , $pcustid=0){
		if(empty($dynamicid) || empty($content)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data = array();
		$data['dynamicid'] = $dynamicid;
		$data['custid'] = $this->_custInfo['custid'];
		$data['content'] = $content;
		$data['pcustid'] = $pcustid;
		$data['type'] = 1;
		$replyid = $this->_dynamic_reply->addReply($data);
		if($replyid){
			//删除动态回复列表缓存
			$dynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['DYNAMIC_REPLY'].$dynamicid;
			$this->_mycache->rm($dynamicReplyKey);
			
			$dynamicInfo = $this->_dynamic->getDynamicById($dynamicid);
			if(!empty($dynamicInfo) && $dynamicInfo['custid'] != $this->_custInfo['custid']){//对好友进行提示
				//与custid相关的回复
				$relationDynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_DYNAMIC_REPLY'].$dynamicInfo['custid'];
				$this->_mycache->sadd($relationDynamicReplyKey , $replyid);
				
				//小红点提示好友有新的相关动态回复或赞
				$msg['from_cust_name'] = $this->_custInfo['nickname'];
				$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
				$msg['type'] = 41;
				$msg['content'] = "";
				\im::sendMsg(3, array($dynamicInfo['custid']), array(), $msg , 1);
			}
			
			if($pcustid && $pcustid != $this->_custInfo['custid']){//对好友进行提示
				//与pcustid相关的回复
				$relationDynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_DYNAMIC_REPLY'].$pcustid;
				$this->_mycache->sadd($relationDynamicReplyKey , $replyid);
				
				//小红点提示好友有新的相关动态回复或赞
				$msg['from_cust_name'] = $this->_custInfo['nickname'];
				$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
				$msg['type'] = 41;
				$msg['content'] = "";
				\im::sendMsg(3, array($pcustid), array(), $msg , 1);
			}
			
			$replyInfo = $this->_dynamic_reply->getReplyById($replyid);
			$custInfo = $this->_getCustInfo($replyInfo['custid']);
			$replyInfo['nickname'] = $custInfo['nickname'];
			
			if(!empty($replyInfo['pcustid'])){
				$custInfo = $this->_getCustInfo($replyInfo['pcustid']);
				$replyInfo['pnickname'] = $custInfo['nickname'];
			}else{
				$replyInfo['pnickname'] = "";
			}
			
			$this->data = $replyInfo;
			$this->outPut();
		}else{
			$this->status = 1611;
			$this->msg = L('STATUS_1611');
			$this->outPut();
		}
	}
	
	
	/**
	 * 删除回复
	 */
	public function deleteReplyAction(){
		$replyid = (int)I('request.replyid');
		$this->_deleteReply($replyid);
	}
	
	private function _deleteReply($replyid){
		if(empty($replyid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$replyInfo = $this->_dynamic_reply->getReplyById($replyid);
		if(!empty($replyInfo)){
			$dynamicid = $replyInfo['dynamicid'];
		}else{
			$this->status = 1613;
			$this->msg = L('STATUS_1613');
			$this->outPut();
		}
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['replyid'] = $replyid;
		$ret = $this->_dynamic_reply->deleteReply($data);
		if($ret){
			//删除动态回复列表缓存
			$dynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['DYNAMIC_REPLY'].$dynamicid;
			$this->_mycache->rm($dynamicReplyKey);
			
			$this->outPut();
		}else{
			$this->status = 1613;
			$this->msg = L('STATUS_1613');
			$this->outPut();
		}
	} 
	
	
	/**
	 * 获取相关动态的回复和赞历史列表
	 */
	public function getRelationListAction(){
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_getRelationList($pageIndex , $pageSize);
	}
	
	private function _getRelationList($pageIndex , $pageSize){
		$relationDynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_DYNAMIC_REPLY'].$this->_custInfo['custid'];
		$replyids = $this->_mycache->smembers($relationDynamicReplyKey);
		if(empty($replyids)){
			$this->outPut();
		}
		
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['replyids'] = $replyids;
		$relationList = $this->_dynamic_reply->getRelationReply($data);
		if(empty($relationList)){
			$this->outPut();
		}
		
		foreach($relationList as &$reply){
			unset($reply['pcustid']);
			$custInfo = $this->_getCustInfo($reply['custid']);
			$reply['nickname'] = $custInfo['nickname'];
			if(!empty($reply['files'])){
				$reply['files'] = json_decode($reply['files'] , true);
				$reply['files'] = $reply['files'][0];
			}
		}
		
		$this->data = $relationList;
		$this->outPut();
	}
	
	
	/**
	 * 删除相关动态的回复和赞历史列表
	 */
	public function delRelationListAction(){
		$replyid = (int)I('request.replyid');
		$this->_delRelationList($replyid);
	}
	
	private function _delRelationList($replyid = 0){
		$relationDynamicReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_DYNAMIC_REPLY'].$this->_custInfo['custid'];
		if($replyid > 0){
			$replyids = $this->_mycache->srem($relationDynamicReplyKey , $replyid);
		}else{
			$replyids = $this->_mycache->rm($relationDynamicReplyKey);
		}
		
		$this->outPut();
	}
	
	
	
	/**
	 * 设置动态背景图
	 */
	public function setBackgroundAction(){
		$this->_setBackground();
	}
	
	private function _setBackground(){
		$ret = $this->_sendFile(false);
		if(empty($ret)){
			$this->status = 1617;
			$this->msg = L('STATUS_1617');
			$this->outPut();
		}
		
		$dynamicsetting = array('bgfileid'=>$ret['fileid'] , 'bgfilehash'=>$ret['filehash']);
		$custInfo['custid'] = $this->_custInfo['custid'];
		$custInfo['dynamicsetting'] = json_encode($dynamicsetting);
		
		if(empty($this->_cust_ext)) $this->_cust_ext = new \Common\Model\CustExtModel();
		$ret = $this->_cust_ext->saveCustExt($custInfo);
		if($ret){
			$this->_setCustInfoCache($custInfo);
			$returnData['custid'] = $this->_custInfo['custid'];
			$returnData['dynamicsetting'] = $dynamicsetting;
			$this->data = $returnData;
			$this->outPut();
		}else{
			$this->status = 1617;
			$this->msg = L('STATUS_1617');
			$this->outPut();
		}
	}
	

	
	/**
	 * 发送文件
	 */
	public function sendFileAction(){
		$this->_sendFile();
	}
	
	
	
	public function getCustImgListAction(){
		$custid = (int)I('request.custid');
		$this->_getCustImgList($custid);
	}
	
	
	private function _getCustImgList($custid){
		if(empty($custid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$result = $this->_dynamic->getDynamicByCust($custid);
		$returnData = array();
		$num = 0;
		if(!empty($result)){
			foreach($result as $dynamic){
				$files = json_decode($dynamic['files'] , true);
				if(!empty($files)){
					if($num > 5) break;
					if(empty($returnData)){
						$returnData = $files;
					}else{
						$returnData = array_merge($returnData , $files);
					}
					$num++;
				}
			}
		}
		
		if(!empty($returnData))$returnData = array_slice($returnData, 0, 5);
		
		$this->data = $returnData;
		$this->outPut();
	}
}
